﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Code4WP7SDK;
using System.Collections.ObjectModel;
using LuWeibo.Models;
using LuWeiboSDK;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Text;
using Code4WP7SDK.Controls;

namespace LuWeibo.ViewModels
{
    public class LuHomePageViewModel : BaseModel
    {
        #region Fields
        private ICommand _showCommentsCommand;
        private ICommand _getAtMineWeiboCommand;
        private ICommand _getSecretInfoCommand;
        private ObservableCollection<Comments> _showComments;
        private ObservableCollection<Status> _atMineStatus;//@我微博
        private ObservableCollection<Status> _favorites;//收藏列表
        private Status _selectedStatus; //选中的微博清除选中项
        private ICommand _goToStatusDetailCommand;
        private ICommand _commentsToStatusDetailCommand;
        private Comments _selectedComments;
        private ICommand _reFreshCommand;
        private ICommand _publishStatusCommand;
        private ICommand _skinManageCommand;
        private ICommand _profileInfoCommand;
        private ICommand _logoutCommand;
        private ICommand _settingCommand;
        #endregion

        #region Properties
        public ObservableCollection<Comments> ShowComments
        {
            get
            {
                if (this._showComments == null)
                {
                    this._showComments = new ObservableCollection<Comments>();
                }
                return _showComments;
            }
            set
            {
                _showComments = value;
                this.NotifyPropertyChanged(p => p.ShowComments);
            }
        }

        public ObservableCollection<Status> AtMineStatus
        {
            get
            {
                if (this._atMineStatus == null)
                {
                    this._atMineStatus = new ObservableCollection<Status>();
                }
                return _atMineStatus;
            }
            set
            {
                _atMineStatus = value;
                this.NotifyPropertyChanged(p => p.AtMineStatus);
            }
        }

        public ObservableCollection<Status> Favorites
        {
            get
            {
                if (this._favorites == null)
                {
                    this._favorites = new ObservableCollection<Status>();
                }
                return _favorites;
            }
            set
            {
                _favorites = value;
                this.NotifyPropertyChanged(p => p.Favorites);
            }
        }

        public Status SelectedStatus
        {
            get { return _selectedStatus; }
            set
            {
                _selectedStatus = value;
                this.NotifyPropertyChanged(p => p.SelectedStatus);
            }
        }

        public Comments SelectedComments
        {
            get { return _selectedComments; }
            set
            {
                _selectedComments = value;
                this.NotifyPropertyChanged(p => p.SelectedComments);
            }
        }
        #endregion

        #region Command


        public ICommand ShowCommentsCommand
        {
            get
            {
                if (this._showCommentsCommand == null)
                {
                    this._showCommentsCommand = new BaseCommand(ShowCommentsExecute);
                }
                return _showCommentsCommand;
            }
        }
        public ICommand GetAtMineWeiboCommand
        {
            get
            {
                if (this._getAtMineWeiboCommand == null)
                {
                    this._getAtMineWeiboCommand = new BaseCommand(GetAtMineWeiboExecute);
                }
                return _getAtMineWeiboCommand;
            }
        }
        public ICommand GetSecretInfoCommand
        {
            get
            {
                if (this._getSecretInfoCommand == null)
                {
                    this._getSecretInfoCommand = new BaseCommand(GetSecretExecute);
                }
                return _getSecretInfoCommand;
            }
        }

        public ICommand GoToStatusDetailCommand
        {
            get
            {
                if (this._goToStatusDetailCommand == null)
                {
                    this._goToStatusDetailCommand = new BaseCommand<Status>(GoToStatusDetailExecute);
                }
                return _goToStatusDetailCommand;
            }
        }

        public ICommand CommentsToStatusDetailCommand
        {
            get
            {
                if (this._commentsToStatusDetailCommand == null)
                {
                    this._commentsToStatusDetailCommand = new BaseCommand<Comments>(CommentsToStatusExecute);
                }
                return _commentsToStatusDetailCommand;
            }
        }

        public ICommand ReFreshCommand
        {
            get
            {
                if (this._reFreshCommand == null)
                {
                    this._reFreshCommand = new BaseCommand(ReFreshExecute);
                }
                return _reFreshCommand;
            }
        }

        public ICommand PublishStatusCommand
        {
            get
            {
                if (this._publishStatusCommand == null)
                {
                    this._publishStatusCommand = new BaseCommand(PublishStatusExecute);
                }
                return _publishStatusCommand;
            }
        }

        public ICommand SkinManageCommand
        {
            get
            {
                if (this._skinManageCommand == null)
                {
                    this._skinManageCommand = new BaseCommand(SkinManageExecute);
                }
                return _skinManageCommand;
            }
        }

        public ICommand ProfileInfoCommand
        {
            get
            {
                if (this._profileInfoCommand == null)
                {
                    this._profileInfoCommand = new BaseCommand(ProfileInfoExecute);
                }
                return _profileInfoCommand;
            }
        }

        public ICommand LogoutCommand
        {
            get
            {
                if (this._logoutCommand == null)
                {
                    this._logoutCommand = new BaseCommand(LogoutExecute);
                }
                return _logoutCommand;
            }
        }

        public ICommand SettingCommand
        {
            get
            {
                if (this._settingCommand == null)
                {
                    this._settingCommand = new BaseCommand(SettingExecute);
                }
                return _settingCommand;
            }
        }
        #endregion

        #region Method
        public void SettingExecute()
        {
            VmNavigateHelper.Navigate("/Views/LuHomePage.xaml", "/Views/Other/SettingPage.xaml");
        }

        public void LogoutExecute()
        {
            NotificationTool.Show
               (
                   "温馨提示",
                   "亲,要先把IE中的Cookie去掉奥!！",
                   new NotificationAction("已去掉", () =>
                   {
                       //清除access_token
                       string access_token = (string)IsoStorageHelper.GetValueFromSettings("access_token");
                       if (!string.IsNullOrEmpty(access_token))
                       {
                           IsoStorageHelper.DeleteSettings("access_token");
                       }
                       VmNavigateHelper.Navigate("/Views/LuHomePage.xaml", "/MainPage.xaml");
                   }),
                   new NotificationAction("查看教程", () =>
                   {
                       VmNavigateHelper.Navigate("/Views/LuHomePage.xaml", "/Views/Other/CookieSettingPage.xaml");
                   })
               );
        }

        public void ProfileInfoExecute()
        {
            VmNavigateHelper.Navigate("/Views/LuHomePage.xaml", "/Views/Profile/UsersStatusPage.xaml?uid=" + Constant.UID);
        }

        public void SkinManageExecute()
        {
            VmNavigateHelper.Navigate("/Views/LuHomePage.xaml", "/Views/Other/SkinsManagePage.xaml");
        }

        public void PublishStatusExecute()
        {
            VmNavigateHelper.Navigate("/Views/LuHomePage.xaml", "/Views/Status/PostStatusPage.xaml");
        }
        public void ReFreshExecute()
        {

        }





        /// <summary>
        /// 获取所有评论
        /// </summary>
        public void ShowCommentsExecute()
        {
            List<Parameter> paras = new List<Parameter>();
            string data = ParameterHelper.BuildParameters(paras);
            WebClientService wcs = new WebClientService();
            wcs.WebClientCompleted += (s, e) =>
                {
                    if (e.Ex == null)
                    {
                        CommentsModel commentsModel = JsonConvert.DeserializeObject<CommentsModel>(e.Result.ToString());
                        this.ShowComments = commentsModel.comments;
                    }
                    else
                    {
                        //出错了
                    }
                };
            wcs.GetRequest(Urls.comments_timeline, data);
        }
        /// <summary>
        /// 获取关于我的微博
        /// </summary>
        public void GetAtMineWeiboExecute()
        {
            //获取@我的微博信息 
            List<Parameter> paras = new List<Parameter>();
            string data = ParameterHelper.BuildParameters(paras);
            WebClientService ws = new WebClientService();
            ws.WebClientCompleted += (s, e) =>
                {
                    if (e.Ex == null)
                    {
                        //Debug.WriteLine("@mestirng:" + e.Result.ToString()); -- 用log来处理
                        StatusModel mineStatus = JsonConvert.DeserializeObject<StatusModel>(e.Result.ToString());
                        this.AtMineStatus = mineStatus.statuses;
                    }
                    else
                    {
                        //获取关于我的信息时出错
                    }
                    
                };
            ws.GetRequest(Urls.statuses_mentions, data);
        }
        /// <summary>
        /// 获取收藏微博
        /// </summary>
        public void GetSecretExecute()
        {
            List<Parameter> paras = new List<Parameter>();
            string data = ParameterHelper.BuildParameters(paras);
            WebClientService ws = new WebClientService();
            ws.WebClientCompleted += (s, e) =>
                {
                    if (e.Ex == null)
                    {
                        using (JsonHelper jhelper = new JsonHelper(e.Result.ToString()))
                        {
                            jhelper.at("favorites");
                            for (int i = 0; i < jhelper.Count(); i++)
                            {
                                Status status = jhelper.at(i).get<Status>("status");
                                //Debug.WriteLine("deleted:" + status.deleted);
                                if (!"1".Equals(status.deleted)) //排除删除微博
                                {
                                    Favorites.Add(status);
                                }
                                jhelper.up();
                            }
                        }
                    }
                    else
                    {
                        //获取收藏失败
                    }
                };
            ws.GetRequest(Urls.favorites, data);
        }

        public void GoToStatusDetailExecute(Status selectedStatus)
        {
            if (selectedStatus != null)
            {
                ViewModelLocator.StatusDetailVMStatic.CurrentStatus = selectedStatus;
                VmNavigateHelper.Navigate("/Views/LuHomePage.xaml", "/Views/Status/StatusDeatailPage.xaml");
                this.SelectedStatus = null;//重置微博列表的选中项目
            }
        }

        public void CommentsToStatusExecute(Comments selectedComments)
        {
            if (selectedComments != null)
            {
                ViewModelLocator.StatusDetailVMStatic.CurrentStatus = selectedComments.status;
                VmNavigateHelper.Navigate("/Views/LuHomePage.xaml", "/Views/Status/StatusDeatailPage.xaml");
                this.SelectedComments = null;
            }
        }
        #endregion
    }
}
